<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <h1 class="title">on origin iframe</h1>
    <button class="btn">子页面按钮</button>
    <script>
      // 非同源情况下：子页面也不能操作父页面
      // console.log(window.parent);
      // console.log(window.parent.document.querySelector("h1"));

      window.onmessage = function (e) {
        console.log("接受到父页面消息", e.data);
      };

      document.querySelector(".btn").onclick = function () {
        window.parent.postMessage("hello ~~~", "http://127.0.0.1:5501");
      };
    </script>
  </body>
</html>
